# -*- encoding: utf-8 -*-
# 
# Lúcuma
# Copyright © 2008 Diego Escalante Urrelo  <diegoe@gnome.org>
# 
# Licensed under the GNU General Public License Version 2
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor
# Boston, MA  02110-1301, USA.
# 

from django.http import HttpResponseRedirect
from lucuma.notes.models import Note
from lucuma.personas.models import Persona
from lucuma.tags.models import _add_tags_to_obj

def add(request):
    """
    Add a new Note.
    """
    if not request.session.get('logged', False):
        return HttpResponseRedirect('/login')
    
    note = Note()
    note.title = request.POST.get('notes_add_title')
    note.body = request.POST.get('notes_add_body')
    note.subject = Persona.objects.get(pk=request.POST.get('notes_add_subject'))
    
    note.author = Persona.objects.get(pk=request.session.get('persona_id'))
    
    if request.POST.get('notes_add_reply') != None:
        note.reply = Note.objects.get(pk=request.POST.get('notes_add_reply'))
    
    note.save()
    
    tags = request.POST.get('notes_add_tags')
    _add_tags_to_obj(note, tags)
    
    return HttpResponseRedirect('/notes/')

def edit(request):
    """
    Edit an existing Note.
    """
    #FIXME: plz, be safer
    if not request.session.get('logged', False):
        return HttpResponseRedirect('/login')
    
    note = Note.objects.get(pk=request.POST.get('notes_single_id'))
    
    if not note:
        return HttpResponseRedirect('/notes/')
    
    # Try to be smart and avoid uneeded update.
    notes_single_title = request.POST.get('notes_single_title', None)
    notes_single_body = request.POST.get('notes_single_body', None)
    notes_single_subject = request.POST.get('notes_single_subject', None)
    notes_single_tags = request.POST.get('notes_single_tags', None)

    if notes_single_title != None and note.title != notes_single_title:
        note.title = notes_single_title 
    
    if notes_single_body != None and note.body != notes_single_body:
        note.body = notes_single_body
    
    if notes_single_subject != None and note.subject != notes_single_subject:
        note.subject = notes_single_subject

    # Django should take care of not doing anything if nothing has been changed.
    note.save()
    
    if notes_single_tags != None:
        tags = notes_single_tags
        _add_tags_to_obj(note, tags)
        
    return HttpResponseRedirect('/notes/%s' % note.id)
    
